package com.happyscan138.makefun;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import java.io.File;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/1/21.
 */

public class viewSpecial extends AppCompatActivity implements View.OnClickListener{

    private GifImageView buttonview;
    private ImageButton back;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_special);
        initView();
        Bundle bundle = new Bundle();
        bundle = this.getIntent().getExtras();
        String a=bundle.getString("special");
        log.e("onSuccess",a);
        File saveImgPath = getImageDir(viewSpecial.this);
        File gifSavePath = new File(saveImgPath, "gif1");
        displayImage(a, gifSavePath, buttonview);

    }
    public void initView(){
        back=(ImageButton)findViewById(R.id.specialView_back);
        back.setOnClickListener(this);
        buttonview=(GifImageView)findViewById(R.id.special_view);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.specialView_back:
                finish();
                break;
        }
    }
    public File getImageDir(Context context){
        File file = getFilesDir(context, "images");
        return file;
    }
    public File getFilesDir(Context context, String tag){
        if(isSdCardExist() == true){
            return context.getExternalFilesDir(tag);
        }
        else{
            return context.getFilesDir();
        }
    }
    public boolean isSdCardExist() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            return true;
        }
        return false;
    }
    public void displayImage(String url, File saveFile, final GifImageView gifView){
        HttpUtils http = new HttpUtils();
        // 下载图片
        http.download(url, saveFile.getAbsolutePath(), new RequestCallBack<File>() {
            public void onSuccess(ResponseInfo<File> responseInfo) {
                try {
                    Log.e("tag", "onSuccess========" + responseInfo.result.getAbsolutePath());
                    GifDrawable gifFrom = new GifDrawable( responseInfo.result.getAbsolutePath() );
                    gifView.setImageDrawable(gifFrom);
                }
                catch(Exception e){
                    Log.e("tag", e.getMessage());
                }
            }

            @Override
            public void onFailure(com.lidroid.xutils.exception.HttpException e, String s) {
                Log.e("tag", "onFailure========" + s);
            }});
    }
}
